/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidVariety;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  查询标的品种列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidVarietyListPlugin implements PluginConnector {

	private IDao bidVarietyDao;
	private Logger logger = Logger.getLogger(BidVarietyListPlugin.class);

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();

		try {
			result.setResult("true");
			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}

			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by variety_code asc ");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidVarietyDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidVarietyDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行BidVarietyList查询失败 ", e);
			result.setMessage("BidVarietyList查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByProperty", "", "", BidVariety.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidVarietyAll"));//获取所有标的品种列表
		if (Validator.isNull(all)) {
			getBidVarietyParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织BidVariety信息查询条件
	private void getBidVarietyParams(MethodParam param, ParamsVo vo) {
		BidVariety bidVariety = new BidVariety();
		bidVariety = (BidVariety) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidVariety);
		if (Validator.isNotNull(bidVariety.getVarietyCode())) {
			param.setParams("varietyCode", bidVariety.getVarietyCode());
		}
		if (Validator.isNotNull(bidVariety.getVarietyName())) {
			param.setParams("varietyName", bidVariety.getVarietyName());
		}
		if (Validator.isNotNull(bidVariety.getVarietyStatus())) {
			param.setParams("varietyStatus", bidVariety.getVarietyStatus());
		}
		if (Validator.isNotNull(bidVariety.getCreateTime())) {
			param.setParams("createTime", bidVariety.getCreateTime());
		}
		if (Validator.isNotNull(bidVariety.getModifyTime())) {
			param.setParams("modifyTime", bidVariety.getModifyTime());
		}
		if (Validator.isNotNull(bidVariety.getSpare1())) {
			param.setParams("spare1", bidVariety.getSpare1());
		}
		if (Validator.isNotNull(bidVariety.getSpare2())) {
			param.setParams("spare2", bidVariety.getSpare2());
		}
		if (Validator.isNotNull(bidVariety.getSpare3())) {
			param.setParams("spare3", bidVariety.getSpare3());
		}
		if (Validator.isNotNull(bidVariety.getSpare4())) {
			param.setParams("spare4", bidVariety.getSpare4());
		}
		if (Validator.isNotNull(bidVariety.getSpare5())) {
			param.setParams("spare5", bidVariety.getSpare5());
		}
	}

	public void setBidVarietyDao(IDao bidVarietyDao) {
		this.bidVarietyDao = bidVarietyDao;
	}

}
